package org.kia.data.model;

import java.io.IOException;
import java.util.List;

import android.os.Parcel;

import com.github.droidfu.cachefu.CachedModel;
import com.github.droidfu.cachefu.ModelCache;
import com.google.gson.annotations.SerializedName;

/**
 * 
 * @author Amir Lazarovich
 */
public class ItemModel extends CategoryModel {

	//////////////////////////////////////////////
	// Members
	//////////////////////////////////////////////
	
	@SerializedName("address")
	protected String mAddress;
	
	@SerializedName("phone")
	protected String mPhone;
	
	@SerializedName("lat")
	protected Double mLatitude;
	
	@SerializedName("long")
	protected Double mLongitude;
	
	@SerializedName("distance")
	protected Integer mDistance;

	@SerializedName("parameters")
	protected List<AccessibilityItem> mAccessibilityParams;
	
	//////////////////////////////////////////////
	// Constructors
	//////////////////////////////////////////////
	
	/**
	 * Constructs a new Item Model
	 */
	public ItemModel() {
		
	}
	
	/**
	 * Construct a new Item Model with given fields
	 * 
	 * @param id
	 * @param name
	 * @param image
	 * @param address
	 * @param phone
	 * @param latitude
	 * @param longitude
	 * @param distance
	 * @param parking
	 * @param entrance
	 * @param elevator
	 */
	public ItemModel(String id, String name, String image, String address, String phone,
			double latitude, double longitude, int distance, List<AccessibilityItem> accessibilityParams) {
		super();
		setId(id);
		this.mName = name;
		this.mImage = image;
		this.mAddress = address;
		this.mPhone = phone;
		this.mLatitude = latitude;
		this.mLongitude = longitude;
		this.mDistance = distance;
		this.mAccessibilityParams =accessibilityParams; 
	}
	
	/**
     * Constructor setting variables from parcel. Same as using a blank constructor and calling readFromParcel.
     *
     * @param source Parcel to be read from.
     * @throws java.io.IOException
     */
    public ItemModel(Parcel source) throws IOException {
        super(source);
    }
	
	//////////////////////////////////////////////
	// Getters & Setters
	//////////////////////////////////////////////

	public String getPhone() {
		return mPhone;
	}

	public void setPhone(String phone) {
		this.mPhone = phone;
	}

	public List<AccessibilityItem> getAccessibilityParams() {
		return mAccessibilityParams;
	}

	public void setAccessibilityParams(List<AccessibilityItem> list) {
		mAccessibilityParams = list;
	}

	public String getAddress() {
		return mAddress;
	}

	public void setAddress(String address) {
		this.mAddress = address;
	}

	public Double getLatitude() {
		return mLatitude;
	}

	public void setLatitude(double latitude) {
		this.mLatitude = latitude;
	}

	public Double getLongitude() {
		return mLongitude;
	}

	public void setLongitude(double longitude) {
		this.mLongitude = longitude;
	}

	public int getDistance() {
		return mDistance;
	}

	public void setDistance(int distance) {
		this.mDistance = distance;
	}
	
//////////////////////////////////////////////
	// Parcel
	//////////////////////////////////////////////

    /**
     * Creator object used for parceling
     */
    public static final Creator<ItemModel> CREATOR = new Creator<ItemModel>() {

        public ItemModel createFromParcel(Parcel source) {
            try {
                return new ItemModel(source);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }

        public ItemModel[] newArray(int size) {
            return new ItemModel[size];
        }
    };
	
	//////////////////////////////////////////////
	// Overrides
	//////////////////////////////////////////////

    @Override
    public boolean reloadFromCachedModel(ModelCache modelCache, CachedModel cachedModel) {
        ItemModel cachedObject = (ItemModel) cachedModel;
        mName = cachedObject.mName;
        mAddress = cachedObject.mAddress;
        mPhone = cachedObject.mPhone;
        mImage = cachedObject.mImage;
        mLatitude = cachedObject.mLatitude;
        mLongitude = cachedObject.mLongitude;
        mDistance = cachedObject.mDistance;
        mAccessibilityParams = cachedObject.mAccessibilityParams;
        return false;
    }

    @SuppressWarnings("unchecked")
	@Override
    public void readFromParcel(Parcel source) throws IOException {
        super.readFromParcel(source);
        mName = source.readString();
        mAddress = source.readString();
        mPhone = source.readString();
        mImage = source.readString();
        mLatitude = source.readDouble();
        mLongitude = source.readDouble();
        mDistance = source.readInt();
        mAccessibilityParams = source.readArrayList(AccessibilityItem.class.getClassLoader());
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeString(mName);
        dest.writeString(mAddress);
        dest.writeString(mPhone);
        dest.writeString(mImage);
        dest.writeDouble((mLatitude != null) ? mLatitude : 0.0);
        dest.writeDouble((mLongitude != null) ? mLongitude : 0.0);
        dest.writeInt((mDistance != null) ? mDistance : 0);
        dest.writeList(mAccessibilityParams);
    }

    @Override
    public String createKey(String id) {
        return id;
    }
	
}
